package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.model.auto.StuNewschoolScheme;
import com.model.auto.StuScheme;
import com.service.IStuNewschoolSchemeService;
import com.service.IStuSchemeService;
import com.util.R;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-10-23
 */
@RestController
@RequestMapping("/stuNewSchoolScheme")
public class StuNewschoolSchemeController{
    @Autowired
    private IStuNewschoolSchemeService stuNewschoolSchemeService;

    //查询方案
    @PostMapping("/getNewSchoolProgrammes")
    public R getNewSchoolProgrammes(String stuId) {
        return R.ok(stuNewschoolSchemeService.getNewSchoolProgrammes(stuId));
    }

    @PostMapping("/getNewSchoolScheme")
    public R getNewSchoolScheme(String stuId, Integer programmeId) {
        System.out.println("--------=======" + stuId);
        System.out.println("--------方案id" + programmeId);
        List<StuNewschoolScheme> list = stuNewschoolSchemeService.list(new LambdaQueryWrapper<StuNewschoolScheme>()
                .eq(!StringUtil.isEmpty(stuId), StuNewschoolScheme::getStuId, stuId)
                .eq(programmeId != null, StuNewschoolScheme::getProgrammeId, programmeId)
        );
        System.out.println("===========结果" + list);
        return R.ok(list);
    }
    @PostMapping("/saveNewSchoolScheme")
    public R saveNewSchoolScheme(@RequestBody StuNewschoolScheme[] stuNewschoolSchemes) {
        for (StuNewschoolScheme scheme : stuNewschoolSchemes) {
            System.out.println("========id" + scheme.getProgrammeId());
            if (scheme.getProgrammeId() == null) {
                Integer maxProgrammeId = stuNewschoolSchemeService.getObj(new QueryWrapper<StuNewschoolScheme>()
                        .select("ifnull(max(programme_id),0) as maxNum")
                        .eq("stu_id", stuNewschoolSchemes[0].getStuId())
                        .orderByDesc("programme_id"), i -> Integer.valueOf(i.toString())
                );
                List<StuNewschoolScheme> schemes=stuNewschoolSchemeService.list(new LambdaQueryWrapper<StuNewschoolScheme>()
                        .eq(StuNewschoolScheme::getStuId, stuNewschoolSchemes[0].getStuId())
                        .orderByDesc(StuNewschoolScheme::getSort).last("limit 1")
                );
                List<StuNewschoolScheme> stuSchemes = Arrays.asList(stuNewschoolSchemes);
                stuSchemes.forEach(i ->{
                    i.setProgrammeId(maxProgrammeId + 1);
                    if(schemes.size()>0){
                        i.setSort(schemes.get(0).getSort()+1);
                    }else {
                        i.setSort(1);
                    }
                });
                long count = stuNewschoolSchemeService.count(new LambdaQueryWrapper<StuNewschoolScheme>().
                        eq(StuNewschoolScheme::getProgrammeName, scheme.getProgrammeName())
                        .eq(StuNewschoolScheme::getStuId, scheme.getStuId())
                );
                if (count > 0) {
                    return R.error("该方案名称已存在！");
                } else {
                    boolean b = stuNewschoolSchemeService.saveBatch(stuSchemes);
                    HashMap<String, Object> object = new HashMap<>();
                    object.put("bol", b);
                    object.put("list", stuSchemes);
                    return R.ok(object);
                }
            } else {
                stuNewschoolSchemeService.remove(new LambdaQueryWrapper<StuNewschoolScheme>()
                        .eq(scheme.getProgrammeId() != null, StuNewschoolScheme::getProgrammeId, scheme.getProgrammeId()));
                List<StuNewschoolScheme> stuSchemes = Arrays.asList(stuNewschoolSchemes);
                System.out.println("stuSchemes" + stuSchemes);
                boolean b = stuNewschoolSchemeService.saveBatch(stuSchemes);
                HashMap<String, Object> object = new HashMap<>();
                object.put("bol", b);
                object.put("list", stuSchemes);
                return R.ok(object);
            }
        }
        return R.ok();
    }

    //修改方案名称
    @PostMapping("/updateName")
    public R updateName(String name, Integer programmeId, String stuId) {
        List<StuNewschoolScheme> schemes = stuNewschoolSchemeService.list(new LambdaQueryWrapper<StuNewschoolScheme>().
                eq(StuNewschoolScheme::getProgrammeName, name)
                .eq(StuNewschoolScheme::getStuId, stuId)
        );
        if (schemes.size()>0&& !Objects.equals(schemes.get(0).getProgrammeId(), programmeId)){
             return R.error("该名称已存在！");
        }
        stuNewschoolSchemeService.update(new LambdaUpdateWrapper<StuNewschoolScheme>()
                .eq(programmeId != null, StuNewschoolScheme::getProgrammeId, programmeId)
                .eq(!StringUtil.isEmpty(stuId), StuNewschoolScheme::getStuId, stuId)
                .set(!StringUtil.isEmpty(name), StuNewschoolScheme::getProgrammeName, name)
        );
        return R.ok();
    }

    @PostMapping("/getPrintNewScheme")
    public R getPrintNewScheme(String stuId) {
        List<Integer> CodeList = new ArrayList<Integer>();
        List<Object> scheme = new ArrayList<Object>();
        List<StuNewschoolScheme> list = stuNewschoolSchemeService.getNewSchoolProgrammes(stuId);
        if (list.size() > 0) {
            list.forEach(i -> CodeList.add(i.getProgrammeId()));
        }
        for (int i = 0; i < CodeList.size(); i++) {
            List<StuNewschoolScheme> lists = stuNewschoolSchemeService.list(new LambdaQueryWrapper<StuNewschoolScheme>()
                    .eq(!StringUtil.isEmpty(stuId), StuNewschoolScheme::getStuId, stuId)
                    .eq(CodeList.get(i) != null, StuNewschoolScheme::getProgrammeId, CodeList.get(i))
            );
            scheme.add(i, lists);
        }
        System.out.println("这个集合" + scheme);
        return R.ok(scheme);
    }

    @GetMapping("/deleteNewStuScheme")
    public R deleteNewStuScheme(Integer programmeId,Integer stuId){
        if (programmeId!=null&&stuId!=null){
            List<StuNewschoolScheme> list=stuNewschoolSchemeService.list(new LambdaQueryWrapper<StuNewschoolScheme>()
                    .eq(StuNewschoolScheme::getStuId,stuId)
                    .eq(StuNewschoolScheme::getProgrammeId,programmeId)
            );
            List<Object> idList=new ArrayList<>();
            if (list.size()>0){
                list.forEach(i->{
                    idList.add(i.getId());
                });
            }
            boolean result=stuNewschoolSchemeService.removeBatchByIds(idList);
            if (result){
               return R.ok();
            }
        }
        return R.error();
    }


    @PostMapping("/UpdateSort")
    public R UpdateSort(@RequestBody StuNewschoolScheme[] stuNewschoolScheme){
        boolean update = stuNewschoolSchemeService.saveOrUpdateBatch(Arrays.asList(stuNewschoolScheme));
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}
